﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditArea : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { Area = o as Area; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(Area); } }
        public string VisualizerName { get { return "EditArea"; } }

        public static readonly DependencyProperty AreaProperty = DependencyProperty.Register(
            "Area", typeof(Area), typeof(EditArea), new PropertyMetadata(null, new PropertyChangedCallback(AreaChanged)));

        public Area Area
        {
            get { return (Area)GetValue(AreaProperty); }
            set { SetValue(AreaProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditArea, T2> action)
        {
            d.HandleChange<EditArea, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void AreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<Area>(d, e, (t, val) => t.Area = val);
        }

        public override void UpdateBindings()
        {
            if (Area != null)
            {
                gridData.DataContext = Area;
            }
        }

        public override string TitleText
        {
            get
            {
                return (Area == null) ? string.Empty :
                    string.Format("Area {0} ({1} Rooms)", Area.ShortName, Area.Rooms.Count);
            }
        }

        public EditArea()
        {
            InitializeComponent();
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreAreaAsync(DataAccess.AssignedUniqueId, Area);
        }

        private void ShowProps_Loaded(object sender, RoutedEventArgs e)
        {
            //editAreaProperties.Window.Indent = Window.Indent + 1;         
        }

        private void EditAssociatedIDCollection_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        private void editGameThing_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }
    }
}